package com.easyse.serviceparticaltasks.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easyse.model.DO.Group;
import com.easyse.model.DO.Task;
import com.easyse.model.DO.User;
import com.easyse.model.vo.ResultVO;
import com.easyse.serviceparticaltasks.mapper.GroupMapper;
import com.easyse.serviceparticaltasks.mapper.TaskMapper;
import com.easyse.serviceparticaltasks.mapper.UserMapper;
import com.easyse.serviceparticaltasks.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：rc
 * @date ：Created in 2022/10/17 23:43
 * @description：
 */

@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    GroupMapper groupMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    TaskMapper taskMapper;


    @Override
    public ResultVO joinGroup(String teamCode, Integer userID) {
        ResultVO resultVO;
        //查询小组代码是哪个小组 得到小组ID
        Map<String,Object> param = new HashMap<>();
        param.put("Invitationcode",teamCode);
        List<Group> groups = groupMapper.selectByMap(param);
        //修改学生信息 小组ID
        //检查小组是否满员
        if(groups.get(0).getNumber() >= groups.get(0).getMaxNumber()){
            resultVO = ResultVO.failure("加入失败，该小组已满员！!");
        }
        if(groups.isEmpty()){
            resultVO = ResultVO.failure("邀请码有误!");
        }else{
            User user = userMapper.selectById(userID);
            if(user.getGroupId() != 0){
                resultVO = ResultVO.failure("您已经有小组了！请先退出您所在的小组！");
            }
            user.setGroupId(Long.valueOf(groups.get(0).getId()));
            int i = userMapper.updateById(user);
            if(i==1){
                //更新小组人数
                groups.get(0).setNumber(groups.get(0).getNumber()+1);
                groupMapper.updateById(groups.get(0));
                resultVO = ResultVO.success("加入小组成功！");
            }else {
                resultVO = ResultVO.failure("发生未知错误，加入小组失败！");
            }
        }
        return resultVO;
    }

    @Override
    public ResultVO quitGroup(Integer userID) {
        ResultVO resultVO;
        User user = userMapper.selectById(userID);
        //要退出小组 只需要设置userID为0
        user.setGroupId(Long.valueOf(0));
        int i = userMapper.updateById(user);
        if(i==1){
                resultVO = ResultVO.success("成功退出小组！");
            }else {
                resultVO = ResultVO.failure("发生未知错误，退出小组失败！");
            }

        return resultVO;
    }

    @Override
    public ResultVO updateGroupInfo(Group group) {
        ResultVO resultVO;
        //更新小组信息，即项目信息
        int i = groupMapper.updateById(group);
        if(i==1){
            resultVO = ResultVO.success("小组项目信息更新成功！");
        }else{
            resultVO = ResultVO.failure("发生未知错误，小组项目信息更新失败！");
        }
        return resultVO;
    }

    @Override

    public ResultVO getProgress(Integer groupID) {
//        Integer groupID = 4;
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Task> qw = queryWrapper.select("group_id")
                .eq("group_id", groupID);
        if(ObjectUtils.isEmpty(taskMapper)){
            System.out.println("对象为空!");
        }
        Long sumNum = taskMapper.selectCount(qw);

        QueryWrapper<Task> queryWrapper2 = new QueryWrapper<>();
        QueryWrapper<Task> qw2 = queryWrapper2.select("group_id")
                .eq("group_id", groupID)
                .eq("finishedBy",null);
        Long unFinishedNum = taskMapper.selectCount(qw2);

        ResultVO resultVO = ResultVO.success("获取任务进度成功！");
        resultVO.data("finishedNum",sumNum - unFinishedNum);
        resultVO.data("unFinishedNum",unFinishedNum);
        return(resultVO);
    }

    @Override

    public ResultVO getGroupInfo(Integer groupID) {
        ResultVO resultVO;
        Group group = groupMapper.selectById(groupID);
        if(ObjectUtils.isEmpty(group)){
            resultVO = ResultVO.failure("获取小组信息失败！");
        }else{
            resultVO = ResultVO.success("获取小组信息成功！");
            resultVO.data("group",group);
        }
        return resultVO;
    }

    @Override
    public ResultVO getGroupList(Integer classID) {
        ResultVO resultVO;
        HashMap<String, Object> map = new HashMap<>();
        map.put("class_ID",classID);
        List<Group> groups = groupMapper.selectByMap(map);

        if(groups.isEmpty()){
            resultVO = ResultVO.failure("该班级小组为空！");
        }else{
            resultVO = ResultVO.success("获取小组列表成功!");
            HashMap<String,Object> groupList=  new HashMap<>();
            groupList.put("groups",groups);
            resultVO.setData(groupList);
        }
        return resultVO;
    }
}
